<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="消息标题" prop="title">
            <el-input v-model="form.title" style="width: 370px;" />
          </el-form-item>
         <!-- <el-form-item label="图片">
            <el-input v-model="form.image" style="width: 370px;" />
          </el-form-item>-->
          <el-form-item label="内容" prop="content">
            <el-input
              style="width: 370px;"
              type="textarea"
              :rows="4"
              placeholder="请输入内容"
              v-model="form.content">
            </el-input>
            <!--<div class="app-container">
              <el-row :gutter="10">
                <el-col :xs="24" :sm="24" :md="15" :lg="15" :xl="15">
                  <div ref="editor" class="text" style="height: 200px"/>
                </el-col>
                <el-col :xs="24" :sm="24" :md="9" :lg="9" :xl="9">
                  <div v-html="editorContent"  style="height: 200px"/>
                </el-col>
              </el-row>
            </div>-->
          </el-form-item>
          <el-form-item label="过期时间">
            <el-date-picker
              v-model="form.expirationTime"
              type="datetime"
              style="width: 370px;"
              placeholder="过期时间">
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="id" />
        <el-table-column prop="title" label="消息标题" />
        <!--<el-table-column prop="image" label="图片" />-->
        <el-table-column prop="content" label="内容" />
        <!--<el-table-column prop="del" label="是否删除" />-->
        <el-table-column prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="expirationTime" label="过期时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.expirationTime) }}</span>
          </template>
        </el-table-column>
        <!--<el-table-column prop="noticeType" label="0群发，1个人" />-->
        <!--<el-table-column prop="userId" label="给个人发送时有值" />-->
        <el-table-column v-permission="['admin','ycNotice:edit','ycNotice:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
  import crudYcNotice from '@/api/notice/notice'
  import CRUD, { presenter, header, form, crud } from '@crud/crud'
  import rrOperation from '@crud/RR.operation'
  import crudOperation from '@crud/CRUD.operation'
  import udOperation from '@crud/UD.operation'
  import pagination from '@crud/Pagination'
  import E from 'wangeditor'

  const defaultForm = { title: null,content: null,expirationTime: null }
  export default {
    name: 'notice',
    components: { pagination, crudOperation, rrOperation, udOperation },
    mixins: [presenter(), header(), form(defaultForm), crud()],
    cruds() {
      return CRUD({ title: '弹窗提示', url: 'api/ycNotice', sort: 'id,desc', crudMethod: { ...crudYcNotice }})
    },
    data() {
      return {
        permission: {
          add: ['admin', 'ycNotice:add'],
          edit: ['admin', 'ycNotice:edit'],
          del: ['admin', 'ycNotice:del']
        },
        rules: {
          title: [
            { required: true, message: '消息标题不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '内容不能为空', trigger: 'blur' }
          ]
        },
        editorContent:'',
      }
    },
    methods: {
      // 钩子：在获取表格数据之前执行，false 则代表不获取数据
      [CRUD.HOOK.beforeRefresh]() {
        this.crud.optShow = {
          add: true,
          edit: true,
          del: true,
          download: false
        }
        return true
      },
      [CRUD.HOOK.beforeToAdd]() {
        this.form.del = 0;
        this.form.createTime = new Date();
        //this.form.expirationTime = new Date();
        return true
      },
    },
    mounted() {
      const _this = this
      var editor = new E(this.$refs.editor)
      // 自定义菜单配置
      editor.customConfig.zIndex = 10
      editor.customConfig.onchange = (html) => {
        this.editorContent = html
      }
      editor.create()
    }
  }
</script>

<style scoped>

</style>
